<template>
  <div>
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="zjm" label="">
          <a-input v-model:value="formState.zjm" class="w-64" placeholder="编号/通知标题/通知内容" @keydown.enter="getData" />
        </a-form-item>
        <a-form-item name="status" label="状态" class="w-72">
          <a-select v-model:value="formState.status" allow-clear placeholder="请选择">
            <a-select-option value="0">待生效</a-select-option>
            <a-select-option value="1">已生效</a-select-option>
            <a-select-option value="2">已过期</a-select-option>
            <a-select-option value="3">已作废</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2">
      <div class="flex justify-start">
        <a-button class="mb-2" type="primary" @click="onAdd"> 新增 </a-button>
      </div>
      <BaseTable fill :data-source="tableData" :loading="loading" :scroll="{ x: 1800 }" column-code="000047" :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'no'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'statusStr'">
            <a-tag v-if="record.status === 0" color="blue">待生效</a-tag>
            <a-tag v-else-if="record.status === 1" color="green">已生效</a-tag>
            <a-tag v-else-if="record.status === 2" color="orange">已过期</a-tag>
            <a-tag v-else color="red">已作废</a-tag>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button v-if="record.status === 1" class="mr-2" type="link" size="small" @click="onEdit(record)">查看</a-button>
            <a-button v-else class="mr-2" type="link" size="small" @click="onEdit(record)">修改</a-button>
            <BaseButton
              confirm-text="是否删除?"
              :disabled="record.status !== 0 && record.status !== 1"
              type="text"
              danger
              size="small"
              @click="onCancel(record)"
            >
              作废
            </BaseButton>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <AddModal v-model:open="open" :row-data="rowData" @ok="getData" />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import AddModal from './AddModal.vue'

const rowData = ref(null)
const formRef = ref(null)
const open = ref(false)
const formState = ref({
  zjm: ''
})
const { loading, tableData, pagination, getData } = useTable({
  url: '/operation/opex/getSystemNoticeList',
  immediate: true,
  params: formState.value
})

const onCancel = async (row) => {
  const { code, msg } = await post('/operation/opex/doCancelSystemNotice', row)
  if (code !== 0) return message.error(msg || '作废失败')
  getData()
  message.success('作废成功')
}
const onEdit = (row) => {
  rowData.value = row
  open.value = true
}
const onAdd = () => {
  rowData.value = {}
  open.value = true
}
</script>
